/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.Controller;

import com.aptech.DAO.userDAO;
import com.aptech.model.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Hoang Quy Hong An
 */
public class changePassword extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            int userID;
            String oldPassword;
            String newPassword;
            String reNewPassword;
            boolean flag = false;
            try{
                //get du lieu:
                userID = Integer.parseInt(request.getParameter("userID"));
                User objUser = new User();
                oldPassword = request.getParameter("oldPassword");
                newPassword = request.getParameter("newPassword");
                reNewPassword = request.getParameter("reNewPassword");
                if(!reNewPassword.equals(newPassword)){
                     HttpSession session = request.getSession(true);
                session.setAttribute("khongtrungPass", 1);
                    response.sendRedirect("changePassword.jsp?userID="+userID);
                }
                //Kiem tra password cu!
                String oldPassMD5 = encodeMD5(oldPassword);
                userDAO objUserDAO = new userDAO();
                objUser.setUserID(userID);
                objUser.setUserPassword(oldPassMD5);
                flag = objUserDAO.checkOldPassword(objUser);
                System.out.println("objUser.getPass: "+objUser.getUserPassword());
                if(flag == true){
                    flag = false;
                    String newPassMD5 = encodeMD5(newPassword);
                    User objNewU = new User();
                    objNewU.setUserID(userID);
                    objNewU.setUserPassword(newPassMD5);
                    flag = objUserDAO.changePassword(objNewU);
                    if(flag == true){
                        HttpSession session = request.getSession(true);
                        session.setAttribute("changPassSuccesss", 1);
                        return mapping.findForward(SUCCESS);
                    }else{
                        HttpSession session = request.getSession(true);
                        session.setAttribute("notChange", 1);
                        return mapping.findForward("hong");
                    }
                }else{
                    HttpSession session = request.getSession(true);
                    session.setAttribute("saiOldPas", 1);
                    response.sendRedirect("changePassword.jsp?userID="+userID);
                    return mapping.findForward("hong");
                }
            }catch(Exception ex){
                ex.printStackTrace();
                return mapping.findForward(ERROR);
            }
    }

    public String encodeMD5(String str){
        String res = "";

        try {
            org.apache.commons.codec.digest.DigestUtils digestUtil  = new DigestUtils();
           res = digestUtil.md5Hex(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return res;

    }
}
